<template>
  <div class="cart">
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th4">单价</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul class="cart-list" v-for="item in $store.state.cart.cartList" :key="item.id" :data-skunum="item.skuNum" :data-skuid="item.skuId"  @click="cartHandler">
          <li class="cart-list-con1">
            <input @change="$store.dispatch('cart/getCheckCartByIdAsync',{
              skuId:item.skuId,
              isChecked:item.isChecked?0:1
                        })" :checked="item.isChecked" type="checkbox" name="chk_list">
          </li>
          <li class="cart-list-con2" @click="$route.push('/detail/'+item.skuId)">
            <img :src="item.imgUrl">
            <div class="item-msg">{{item.skuName}}</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">{{item.skuPrice|currency}}</span>
          </li>
          <li class="cart-list-con5">
            <a data-n="-1" href="javascript:void(0)" class="mins">-</a>
        <input @change="changeBuyNum(item.skuId,item.skuNum,$event)" autocomplete="off" type="text" :value="item.skuNum" minnum="1" class="itxt">
        <a data-n="1" href="javascript:void(0)" class="plus">+</a>
          </li>
          <li class="cart-list-con6">
            <span class="sum">{{item.skuPrice*item.skuNum|currency}}</span>
          </li>
          <li class="cart-list-con7">
            <a href="javascript:;" class="sindelet" :date-skuid="item.skuId">删除</a>
            <br>
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" @click="$store.dispatch('cart/postBatchCheckCartAsync')"
          :checked="$store.getters['cart/isAll']">
        <span>全选</span>
      </div>
      <div class="option" v-show="$store.getters['cart/isChecked']">
        <a @click="$store.dispatch('cart/deleteIsCheckedAsync',$store.state.cart.cartList.filter(item=>item.isChecked).map(item=>item.skuId))"
          href="javascript:;">删除选中的商品</a>
      </div>
      <div class="money-box">
                <div class="chosed">已选择
                    <span>{{$store.getters["cart/getNumAndPrice"].num}}</span>件商品</div>
                <div class="sumprice">
                    <em>总价（不含运费） ：</em>
                    <i class="summoney">{{$store.getters["cart/getNumAndPrice"].price | currency}}</i>
                </div>
                <div class="sumbtn">
                    <router-link class="sum-btn" to="/trade">结算</router-link>
                </div>
            </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Cart',
  methods:{
        async changeBuyNum(skuId,skuNum,e){
            let changeNum = e.target.value.trim()/1;
            if(!goodsNumReg.test(changeNum)){
                e.target.value = skuNum
            }
            skuNum = changeNum-skuNum;
            await this.$store.dispatch("cart/postAddToCartAsync",{
                skuId,
                skuNum
            });
            await this.$store.dispatch("cart/getCartListAsync")
        },
        async cartHandler(e){
            // target:点击的元素
            // currentTarget:事件定义的元素
            // 判断是否点击了加减按钮
            if(e.target.classList.contains("mins") || e.target.classList.contains("plus")){
                // 判断是否有锁。
                if(this.isLock) return;
                // 加锁
                this.isLock = true;
                // 当前的商品数量
                let skuNum = e.currentTarget.dataset.skunum/1;
                // 获取操作的数量
                let changeNum = e.target.dataset.n/1;
                // 计算更改后的数据
                skuNum+=changeNum;
                // skuNum小于1,删除数据
                if(skuNum<1){
                    // 删除该数据
                    await this.$store.dispatch("cart/deleteCartBySkuIdAsync",e.currentTarget.dataset.skuid);
                    this.isLock=false;
                    return;
                }else if(skuNum>200){
                    // skuNum大于200
                    this.isLock = false;
                    return;
                }
                // 更新购物车中的商品数量

                await this.$store.dispatch("cart/postAddToCartAsync",{
                    skuId:e.currentTarget.dataset.skuid,
                    skuNum:changeNum
                });
                await this.$store.dispatch("cart/getCartListAsync");
                this.isLock = false;


            }


            // if(e.target.classList.contains("mins")){
            //     // 看门狗
            //     if(this.isLock) return;
            //     this.isLock = true;
            //     let skuNum = e.currentTarget.dataset.skunum/1;
            //     // 判断skuNum的值减去1之后是否小于1
            //     if(skuNum<=1){
            //         await this.$store.dispatch("cart/deleteCartBySkuIdAsync",e.currentTarget.dataset.skuid)
            //     }else{
            //         await this.$store.dispatch("cart/postAddToCartAsync",{
            //             skuId:e.currentTarget.dataset.skuid,
            //             skuNum:-1
            //         });
            //         await this.$store.dispatch("cart/getCartListAsync");
            //     };
            //     this.isLock = false;
            //
            // }
            // if(e.target.classList.contains("plus")){
            //     // 看门狗
            //     if(this.isLock) return;
            //     this.isLock = true;
            //     // console.log(e.currentTarget.dataset.skuid);
            //     let skuNum = e.currentTarget.dataset.skunum/1;
            //     if(skuNum===200) return;
            //
            //     await this.$store.dispatch("cart/postAddToCartAsync",{
            //         skuId:e.currentTarget.dataset.skuid,
            //         skuNum:skuNum>200?200-skuNum:1
            //     });
            //     await this.$store.dispatch("cart/getCartListAsync");
            //     this.isLock=false;
            // }
            // 删除
            if(e.target.classList.contains("sindelet") && window.confirm("确定要删除该信息吗？")){
                const skuId = e.currentTarget.dataset.skuid;
                this.$store.dispatch("cart/deleteCartBySkuIdAsync",skuId);

            }
        }
    },
  mounted () {
    this.$store.dispatch('cart/getCartListAsync')
  },
}

</script>

<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;

  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }

  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;

      & > div {
        float: left;
      }

      .cart-th1 {
        width: 25%;

        input {
          vertical-align: middle;
        }

        span {
          vertical-align: middle;
        }
      }

      .cart-th2 {
        width: 25%;
      }

      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6 {
        width: 12.5%;
      }
    }

    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;

      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;

        & > li {
          float: left;
        }

        .cart-list-con1 {
          width: 15%;
        }

        .cart-list-con2 {
          width: 35%;

          img {
            width: 82px;
            height: 82px;
            float: left;
          }

          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }

        .cart-list-con4 {
          width: 10%;
        }

        .cart-list-con5 {
          width: 17%;

          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }

          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 33px;
            float: left;
            text-align: center;
            font-size: 14px;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
        }

        .cart-list-con6 {
          width: 10%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 13%;

          a {
            color: #666;
          }
        }
      }
    }
  }

  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: "Microsoft YaHei";
          background: #e1251b;
          overflow: hidden;
        }
      }
    }
  }
}
</style>